<?php
session_start();
if(isset($_SESSION['lang']))
    require_once "./language_files/inviteOrganizers_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/inviteOrganizers_en.php";
require_once "functions.php";
if(isset($_POST['word']))
{
    $word=$_POST['word'];
    $member_id=$_SESSION['member_id'];
    $conference_id=intval($_POST['confID']);
    $O=$_POST['role'];
    $privilege=$_POST['priv'];
    $role=$_POST['r'];
    if($_POST['word'] == "")
    {
        $members_list=mysql_query("SELECT DISTINCT m.member_id, m.first_name, m.last_name
                FROM member m
                WHERE m.member_id != '$member_id'
                AND m.member_id NOT IN (SELECT member_id2 FROM member_request_member
                WHERE type = '$O' AND conference_id = '$conference_id' AND approve is NULL)
                AND m.member_id NOT IN (SELECT member_id FROM member_privileges WHERE conference_id = '$conference_id'
                AND privileges_id = '$privilege') ORDER BY last_name ASC") or die(mysql_error());
    }
    else
    {
        $members_list=mysql_query("SELECT DISTINCT  m.member_id, m.first_name, m.last_name
                FROM member m
                WHERE m.member_id != '$member_id'
                AND m.member_id NOT IN (SELECT member_id2 FROM member_request_member
                WHERE type = '$O' AND conference_id = '$conference_id' AND approve is NULL)
                AND m.member_id NOT IN (SELECT member_id FROM member_privileges WHERE conference_id = '$conference_id'
                AND privileges_id = '$privilege')
            AND (first_name LIKE '$word' OR first_name LIKE '$word%' OR
            last_name LIKE '$word' OR last_name LIKE '$word%'
                OR CONCAT(m.first_name,' ', m.last_name) LIKE '%$word%')") or die(mysql_error());
    }
    echo "<div id='container'>";

    echo "<table class='no_border'>";
//count the number of records of the previouse query
    $no_row=mysql_num_rows($members_list);
// if there are no records resulted from the query, display a message
    if($no_row == 0)
        die($lang['error_1']);
// a counter that its value is added to the check box and dropdown list for every member in the list to keep track of their ids to know later which of them checked to get the id of its member
    $i=0;
// loops on each member in the list by making every row in the selected table in an array
    while($line=mysql_fetch_assoc($members_list))
    {
        // get all committees that are in this conference
        $committees_inconference=mysql_query("Select c.committee_id, c.committee_name From committees c, conference_committee s
											WHERE c.committee_id = s.committee_id AND s.conference_id = '$conference_id'")
                or die(mysql_error());
        // get number of rows
        $rows=mysql_num_rows($committees_inconference);
        // print the name of each member
        //generate check box with the id of its member_id and when checked it calls the function that enables the dropdown list
        // generate disabled dropdown list also with the value of its the member id and an id which is the counter (its number in the list)
        if($role == "organizer" || $role == "reviewer")
        {
            echo '<tr><td><input type="checkbox" name="members[]" value="' . $line['member_id'] . '" onClick="enable(this, ' . $i . ')"> ' . $line['first_name'] . ' ' . $line['last_name'] . '</td>
				   <td><select class="' . $line['member_id'] . '" value="" id="Committees' . $i . '" name="Committees[]" disabled="disabled">';
            // incrementing the counter
            $i++;
            // loop for populating the dropdown list
            for($j=0; $j < $rows; $j++)
            {
                //get the committee name with a counter which is considered its id, it comes second in the array
                $commName=mysql_result($committees_inconference,$j,1);
                //get the committee id with a counter which is considered its id, it comes first in the array
                $commId=mysql_result($committees_inconference,$j,0);
                // add the committee name to the dropdown list, committee id is its value
                echo '<option value="' . $commId . '">' . $commName . '</option>';
            }

            echo '</select></br></td></tr>';
        }
        else
        {
            echo '<tr><td><input type="checkbox" name="members[]" value="' . $line['member_id'] . '" onClick="enable(this, ' . $i . ')"> ' . $line['first_name'] .
            ' ' . $line['last_name'] . "<br /></td></tr>";
        }
    }
    echo "<tr><td colspan='10'><input type ='submit' name ='Done' value ='{$lang['button_1']}' /></td></tr>";
    echo "</table>";
}
?>